package openfidias.maker.model

import openfidias.maker.format.Formatter as f
import org.apache.commons.lang.StringUtils as sutils

/**
 *
 * @author atila
 */
class ModelMaker {

   /**
    * @param a model.
    */
   def make(String value) {

      def model = stringToModel(value)

      def source = new StringBuilder()
      source.append(model.header())
      source.append(model.definition())
      source.append(model.classAttr())
      source.append(model.gettersAndSetters())
      source.append(f.endClass)
      println source.toString()
      return model
   }

   /**
    * String format:
    *
    *    org.fidias.pessoa nome:String fone:long
    */
   def private stringToModel(String value) {
      def source = value.tokenize()

      def attrs = []
      source.tail().each {
         def element = it.tokenize(":")
         attrs.push( new Attribute(name:element[0], type:element[1]) )
      }

      def rawClass = source[0]

      /*def last = rawClass.lastIndexOf(".")
      def c = f.uncap( rawClass.substring( last.plus(1) ) )
      def p = rawClass.substring(0, last)*/
      def c = f.uncap( sutils.substringAfterLast(rawClass, ".") )
      def p = sutils.substringBeforeLast(rawClass, ".")

      def model = new Model(_class:c, _package:p, attributes:attrs)
      return model
   }
}

